package com.umeng.soexample.mvp.view.fragment;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.umeng.lib_core.base.BaseFragment;
import com.umeng.soexample.R;
import com.umeng.soexample.bean.SelUserEntity;
import com.umeng.soexample.bean.UpdateUserEntity;
import com.umeng.soexample.bean.UploadHeadEntity;
import com.umeng.soexample.dao.USerService;
import com.umeng.soexample.di.compont.DaggerSelUserFragmentCompont;
import com.umeng.soexample.di.modules.SelUserFragmentMiodules;
import com.umeng.soexample.eventbus_bean.BaseEventBusEntity;
import com.umeng.soexample.eventbus_bean.UploadHeadBus;
import com.umeng.soexample.mvc.view.FragmentActivity;
import com.umeng.soexample.mvp.contract.SelUserFragmentContract;
import com.umeng.soexample.mvp.persenter.SelUserFragmentPresenter;
import com.umeng.soexample.mvp.view.custom.wheel.WheelView;
import com.umeng.soexample.mvp.view.custom.wheel.adapters.ArrayWheelAdapter;
import com.umeng.soexample.network.API;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * 个人信息
 */
public class SelUserFragment extends BaseFragment<SelUserFragmentPresenter> implements SelUserFragmentContract.SelUserFragmentIView, View.OnClickListener {

    private SelUserEntity.ValuesBean valuesBean;

    private View view;
    //几块布局控件
    private LinearLayout ll_sel_name_grxx;
    private LinearLayout ll_sel_name_nc;
    private LinearLayout ll_sel_name_wdclkl;
    private LinearLayout ll_sel_name_height;
    //个人信息控件
    private ImageView iv_sel_user_user_head;
    private TextView tv_sel_user_nick_name;
    private TextView tv_sel_user_user_sex;
    private TextView tv_sel_user_user_birthday;
    private TextView tv_sel_user_user_height;
    private TextView tv_sel_user_user_weight;
    private ImageView iv_sel_user_user_vip;
    private TextView tv_sel_user_clkl;
    //昵称
    private ImageView iv_sel_user_name_close;
    private EditText et_sel_user_name;
    //我的潮流口令
    private EditText et_sel_user_clkl;
    private TextView tv_sel_user_clkl_num;
    //头像
    private PopupWindow head_pop;
    //性别
    private PopupWindow sex_pop;
    //日期
    private PopupWindow date_pop;
    private DatePicker dp_item_bottom_date;
    //身高/体重
    private boolean HorW = false;//false 为身高   true 为体重
    private WheelView wv_sel_user_height;//身高
    private List<String> heights = new ArrayList<>();
    private WheelView wv_sel_user_weight;//身高
    private List<String> weights = new ArrayList<>();
    private File file;

    @Override
    public View createView(LayoutInflater inflater) {
        view = inflater.inflate(R.layout.fragment_sel_user, null);
        EventBus.getDefault().register(this);
        return view;
    }

    @Override
    public void init(View view) {
        //初始化布局控件
        ll_sel_name_grxx = view.findViewById(R.id.ll_sel_name_grxx);
        ll_sel_name_nc = view.findViewById(R.id.ll_sel_name_nc);
        ll_sel_name_wdclkl = view.findViewById(R.id.ll_sel_name_wdclkl);
        ll_sel_name_height = view.findViewById(R.id.ll_sel_name_height);
        //初始化个人信息控件
        iv_sel_user_user_head = view.findViewById(R.id.iv_sel_user_user_head);
        tv_sel_user_nick_name = view.findViewById(R.id.tv_sel_user_nick_name);
        iv_sel_user_user_vip = view.findViewById(R.id.iv_sel_user_user_vip);
        tv_sel_user_clkl = view.findViewById(R.id.tv_sel_user_clkl);
        tv_sel_user_user_sex = view.findViewById(R.id.tv_sel_user_user_sex);
        tv_sel_user_user_birthday = view.findViewById(R.id.tv_sel_user_user_birthday);
        tv_sel_user_user_height = view.findViewById(R.id.tv_sel_user_user_height);
        tv_sel_user_user_weight = view.findViewById(R.id.tv_sel_user_user_weight);
        //初始化昵称页控件
        iv_sel_user_name_close = view.findViewById(R.id.iv_sel_user_name_close);
        et_sel_user_name = view.findViewById(R.id.et_sel_user_name);
        //初始化我的潮流口令控件
        et_sel_user_clkl = view.findViewById(R.id.et_sel_user_clkl);
        tv_sel_user_clkl_num = view.findViewById(R.id.tv_sel_user_clkl_num);
        //初始化身高体重控件
        for (int i = 50; i < 251; i++){
            heights.add("" + i);
        }
        for (int i = 10; i < 141; i++){
            weights.add("" + i);
        }
        wv_sel_user_height = view.findViewById(R.id.wv_sel_user_height);
        wv_sel_user_weight = view.findViewById(R.id.wv_sel_user_weight);
        wv_sel_user_height.setViewAdapter(new ArrayWheelAdapter(getActivity(), heights));
        wv_sel_user_weight.setViewAdapter(new ArrayWheelAdapter(getActivity(), weights));

    }

    @Override
    public void initData() {
        Cursor cursor = USerService.getInstance().query("user", null, null);
        if (cursor.moveToNext()) {
            String user_id = cursor.getString(cursor.getColumnIndex("user_id"));
            Toast.makeText(getContext(), user_id, Toast.LENGTH_SHORT).show();
            p.selUserRequest(user_id);
        }


    }


    @Override
    public void setupDagger() {
        DaggerSelUserFragmentCompont.builder().selUserFragmentMiodules(new SelUserFragmentMiodules(this))
                .build().inject(this);
    }

    @Override
    public void initListener() {
        //添加个人信息监听
        view.findViewById(R.id.iv_sel_user_back).setOnClickListener(this);//退出个人信息页
        view.findViewById(R.id.ll_sel_user_user_head_more).setOnClickListener(this);//头像
        view.findViewById(R.id.ll_sel_user_nick_name_more).setOnClickListener(this);//昵称
        view.findViewById(R.id.ll_sel_user_user_vip_more).setOnClickListener(this);//会员等级
        view.findViewById(R.id.ll_sel_user_qr_code_more).setOnClickListener(this);//我的二维码
        view.findViewById(R.id.ll_sel_user_clkl_more).setOnClickListener(this);//我的潮流口令
        view.findViewById(R.id.ll_sel_user_user_sex_more).setOnClickListener(this);//性别
        view.findViewById(R.id.ll_sel_user_user_birthday_more).setOnClickListener(this);//生日
        view.findViewById(R.id.ll_sel_user_user_height_more).setOnClickListener(this);//身高
        view.findViewById(R.id.ll_sel_user_user_weight_more).setOnClickListener(this);//体重
        //添加昵称监听
        view.findViewById(R.id.iv_sel_user_name_back).setOnClickListener(this);//返回个人信息
        view.findViewById(R.id.tv_sel_user_name_share).setOnClickListener(this);//保存
        view.findViewById(R.id.iv_sel_user_name_close).setOnClickListener(this);//清空
        et_sel_user_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    iv_sel_user_name_close.setVisibility(View.VISIBLE);
                } else {
                    iv_sel_user_name_close.setVisibility(View.GONE);
                }
            }
        });
        //添加我的潮流口令监听
        view.findViewById(R.id.iv_sel_user_clkl_back).setOnClickListener(this);//返回个人信息
        view.findViewById(R.id.tv_sel_user_clkl_share).setOnClickListener(this);//保存
        et_sel_user_clkl.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int num = 15 - s.length();
                if (num <= 0){
                    et_sel_user_clkl.setText(s.subSequence(0, s.length() - 1));
                }
                tv_sel_user_clkl_num.setText("" + num);
            }
        });
        //添加身高体重监听
        view.findViewById(R.id.btn_sel_user_hw_share).setOnClickListener(this);//保存
        view.findViewById(R.id.iv_sel_user_hw_back).setOnClickListener(this);////返回个人信息

    }


    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    /**
     * 查询用户信息返回接口
     * @param entity
     */
    @Override
    public void SelUserSuccess(SelUserEntity entity) {
        if (entity.values == null || entity.values.size() == 0) {
            return;
        }
        Toast.makeText(getContext(), entity.msg, Toast.LENGTH_SHORT).show();
        valuesBean = entity.values.get(0);

        Glide.with(getActivity()).load(API.BaseUrl + entity.values.get(0).user_head)
                .asBitmap()
                .centerCrop()
                //禁用磁盘缓存
                .diskCacheStrategy( DiskCacheStrategy.NONE)
                //跳过内存缓存
                .skipMemoryCache(true)
                .into(iv_sel_user_user_head);//头像
       // Glide.with(this).load(API.BaseUrl + entity.values.get(0).user_head).crossFade(1000).into(iv_sel_user_user_head);

                Glide.with(getContext()).load(API.BaseUrl + entity.values.get(0).user_head)
                .asBitmap()
                .centerCrop()
                //禁用磁盘缓存
                .diskCacheStrategy( DiskCacheStrategy.NONE)
                //跳过内存缓存
                .skipMemoryCache(true)
                .into(iv_sel_user_user_head);


        Glide.with(this).load(API.BaseUrl + entity.values.get(0).user_head).bitmapTransform(new CropCircleTransformation(getContext())).crossFade(1000).into(iv_sel_user_user_head);




        tv_sel_user_nick_name.setText(entity.values.get(0).nick_name);
        tv_sel_user_user_sex.setText(entity.values.get(0).user_sex);
        tv_sel_user_user_birthday.setText(entity.values.get(0).user_birthday);
        tv_sel_user_user_height.setText(entity.values.get(0).user_height);
        tv_sel_user_user_weight.setText(entity.values.get(0).user_weight);


        /**
         * 修改数据裤
         */
        String user_id = "";
        Cursor cursor = USerService.getInstance().query("user", null, null);
        if (cursor.moveToNext()){
            user_id = cursor.getString(cursor.getColumnIndex("user_id"));
        }else{
            Toast.makeText(getContext(), "个人信息 -> 查询用户信息 -> 数据库搜索失败", Toast.LENGTH_SHORT).show();
            return;
        }
        ContentValues values = new ContentValues();
        values.put("user_id", valuesBean.user_id);
        values.put("nick_name", valuesBean.nick_name);
        values.put("qr_code", valuesBean.qr_code);
        values.put("user_name", valuesBean.user_name);
        values.put("user_pwd", valuesBean.user_pwd);
        values.put("user_sex", valuesBean.user_sex);
        values.put("user_birthday", valuesBean.user_birthday);
        values.put("user_height", valuesBean.user_height);
        values.put("user_weight", valuesBean.user_weight);
        values.put("user_head", valuesBean.user_head);
        int update = USerService.getInstance().update("user", values, "user_id=?", new String[]{user_id});
        if (update <= 0){
            Toast.makeText(getContext(), "个人信息  -> 数据库修改失败", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 修改个人信息返回接口
     * @param entity
     */
    @Override
    public void UpdateUserSuccess(UpdateUserEntity entity) {
        Toast.makeText(getContext(), "个人信息 -> 修改个人信息返回接口 -> " + entity.msg, Toast.LENGTH_SHORT).show();
        if ("1".equals(entity.statues)){
            initData();
        }
    }

    /**
     * 上传头像返回接口
     * @param entity
     */
    @Override
    public void UploadHeadSuccess(UploadHeadEntity entity) {
        Toast.makeText(getContext(), "个人信息 -> 上传头像返回接口 -> " + entity.msg, Toast.LENGTH_SHORT).show();
        if ("1".equals(entity.statues)){
            initData();
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //TODO 个人信息
            case R.id.iv_sel_user_back://退出个人信息页
                getActivity().finish();
                break;
            case R.id.ll_sel_user_user_head_more://头部
                head_popupwindow();
                break;
            case R.id.ll_sel_user_nick_name_more://昵称
                ll_sel_name_grxx.setVisibility(View.GONE);
                ll_sel_name_nc.setVisibility(View.VISIBLE);
                et_sel_user_name.setText(tv_sel_user_nick_name.getText().toString());
                break;
            case R.id.ll_sel_user_user_vip_more://VIP
                break;
            case R.id.ll_sel_user_qr_code_more://二维码
                Intent zxing = new Intent(getActivity(), FragmentActivity.class);
                zxing.putExtra("type", "zxing");
                getActivity().startActivity(zxing);
                break;
            case R.id.ll_sel_user_clkl_more://潮流口令
                ll_sel_name_grxx.setVisibility(View.GONE);
                ll_sel_name_wdclkl.setVisibility(View.VISIBLE);
                et_sel_user_clkl.setText(tv_sel_user_clkl.getText().toString().trim());
                break;
            case R.id.ll_sel_user_user_sex_more://性别
                sex_popupwindow();
                break;
            case R.id.ll_sel_user_user_birthday_more://生日
                date_popupwindow();
                break;
            case R.id.ll_sel_user_user_height_more://身高
                ll_sel_name_grxx.setVisibility(View.GONE);
                ll_sel_name_height.setVisibility(View.VISIBLE);
                HorW = false;
                break;
            case R.id.ll_sel_user_user_weight_more://体重
                ll_sel_name_grxx.setVisibility(View.GONE);
                ll_sel_name_height.setVisibility(View.VISIBLE);
                HorW = true;
                break;
            //TODO 头像
            case R.id.tv_item_bottom_head_photograph://拍照
                startCamera();
                if (head_pop != null){
                    head_pop.dismiss();
                }
                break;
            case R.id.tv_item_bottom_head_photo://本地相册
                startPhoto();
                if (head_pop != null){
                    head_pop.dismiss();
                }
                break;
            case R.id.tv_item_bottom_head_qx://取消
                if (head_pop != null){
                    head_pop.dismiss();
                }
                break;
            //TODO 昵称
            case R.id.iv_sel_user_name_back://返回个人信息
                ll_sel_name_nc.setVisibility(View.GONE);
                ll_sel_name_grxx.setVisibility(View.VISIBLE);
                break;
            case R.id.tv_sel_user_name_share://保存
                if (!TextUtils.isEmpty(et_sel_user_name.getText().toString().trim())){
                    valuesBean.nick_name = et_sel_user_name.getText().toString().trim();
                    updateUser();
                    ll_sel_name_nc.setVisibility(View.GONE);
                    ll_sel_name_grxx.setVisibility(View.VISIBLE);
                }else{
                    Toast.makeText(getContext(), "个人信息 -> 昵称不能为空!", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.iv_sel_user_name_close://清空
                et_sel_user_name.setText("");
                break;
            //TODO 我的潮流口令
            case R.id.iv_sel_user_clkl_back://返回个人信息
                ll_sel_name_wdclkl.setVisibility(View.GONE);
                ll_sel_name_grxx.setVisibility(View.VISIBLE);
                break;
            case R.id.tv_sel_user_clkl_share://保存
                tv_sel_user_clkl.setText(et_sel_user_clkl.getText().toString().trim());
                ll_sel_name_wdclkl.setVisibility(View.GONE);
                ll_sel_name_grxx.setVisibility(View.VISIBLE);
                break;
            //TODO 性别
            case R.id.tv_item_bottom_sex_boy://男
                valuesBean.user_sex = "BOY";
                updateUser();
                if (sex_pop != null){
                    sex_pop.dismiss();
                }
                break;
            case R.id.tv_item_bottom_sex_girl://女
                valuesBean.user_sex = "GIRL";
                updateUser();
                if (sex_pop != null){
                    sex_pop.dismiss();
                }
                break;
            case R.id.tv_item_bottom_sex_qx://取消
                if (sex_pop != null){
                    sex_pop.dismiss();
                }
                break;
            //TODO 生日
            case R.id.tv_item_bottom_date_qx://取消
                if (date_pop != null){
                    date_pop.dismiss();
                }
                break;
            case R.id.tv_item_bottom_date_sure://确定
                if (date_pop != null){
                    valuesBean.user_birthday = dp_item_bottom_date.getYear() + "-" + (dp_item_bottom_date.getMonth() + 1) + "-" + dp_item_bottom_date.getDayOfMonth();
                    date_pop.dismiss();
                }
                updateUser();
                break;
            //TODO 身高/体重
            case R.id.btn_sel_user_hw_share://保存
                if (HorW){//体重
                    valuesBean.user_weight = weights.get(wv_sel_user_weight.getCurrentItem()) + "KG";
                    valuesBean.user_height = heights.get(wv_sel_user_height.getCurrentItem()) + "CM";
                    updateUser();
                }else{//身高
                    valuesBean.user_height = heights.get(wv_sel_user_height.getCurrentItem()) + "CM";
                    valuesBean.user_weight = weights.get(wv_sel_user_weight.getCurrentItem()) + "KG";
                    updateUser();
                }
                ll_sel_name_height.setVisibility(View.GONE);
                ll_sel_name_grxx.setVisibility(View.VISIBLE);
                break;
            case R.id.iv_sel_user_hw_back://返回个人信息
                ll_sel_name_height.setVisibility(View.GONE);
                ll_sel_name_grxx.setVisibility(View.VISIBLE);
                break;
        }
    }

    //头像底部弹出框
    public void head_popupwindow() {
        //加入购物车控件
        //TODO 1:实例化对象
        head_pop = new PopupWindow(getActivity());
        //TODO 2：设置属性
        View view_head = LayoutInflater.from(getActivity()).inflate(R.layout.item_bottom_head, null);
        //添加监听事件
        view_head.findViewById(R.id.tv_item_bottom_head_photograph).setOnClickListener(this);
        view_head.findViewById(R.id.tv_item_bottom_head_photo).setOnClickListener(this);
        view_head.findViewById(R.id.tv_item_bottom_head_qx).setOnClickListener(this);

        head_pop.setContentView(view_head);
        head_pop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        head_pop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置点击外部消失
        head_pop.setOutsideTouchable(true);
        //TODO 3:展示
        /**
         * @param parent 父布局
         * @param gravity gravity可以是Gravity.TOP、Gravity.BOTTOM、Gravity.LEFT、Gravity.RIGHT。。。。
         * @param x x轴偏移量
         * @param y y轴偏移量
         */
        View parent = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_goods_values, null);

        //1.获得当前窗体的属性
        WindowManager.LayoutParams layoutParams = getActivity().getWindow().getAttributes();
        //2.设置透明度为0.5f,半透明状态
        layoutParams.alpha=0.5f;
        //3.为窗体设置新属性
        getActivity().getWindow().setAttributes(layoutParams);
        //4.当窗体消失的时候，恢复透明度
        head_pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams layoutParams = getActivity().getWindow().getAttributes();
                layoutParams.alpha=1f;
                getActivity().getWindow().setAttributes(layoutParams);
            }
        });
        head_pop.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
    }
    //性别底部弹出框
    public void sex_popupwindow() {
        //加入购物车控件
        //TODO 1:实例化对象
        sex_pop = new PopupWindow(getActivity());
        //TODO 2：设置属性
        View view_sex = LayoutInflater.from(getActivity()).inflate(R.layout.item_bottom_sex, null);
        //添加监听事件
        view_sex.findViewById(R.id.tv_item_bottom_sex_boy).setOnClickListener(this);
        view_sex.findViewById(R.id.tv_item_bottom_sex_girl).setOnClickListener(this);
        view_sex.findViewById(R.id.tv_item_bottom_sex_qx).setOnClickListener(this);

        sex_pop.setContentView(view_sex);
        sex_pop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        sex_pop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置点击外部消失
        sex_pop.setOutsideTouchable(true);
        //TODO 3:展示
        /**
         * @param parent 父布局
         * @param gravity gravity可以是Gravity.TOP、Gravity.BOTTOM、Gravity.LEFT、Gravity.RIGHT。。。。
         * @param x x轴偏移量
         * @param y y轴偏移量
         */
        View parent = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_goods_values, null);

        //1.获得当前窗体的属性
        WindowManager.LayoutParams layoutParams = getActivity().getWindow().getAttributes();
        //2.设置透明度为0.5f,半透明状态
        layoutParams.alpha=0.5f;
        //3.为窗体设置新属性
        getActivity().getWindow().setAttributes(layoutParams);
        //4.当窗体消失的时候，恢复透明度
        sex_pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams layoutParams = getActivity().getWindow().getAttributes();
                layoutParams.alpha=1f;
                getActivity().getWindow().setAttributes(layoutParams);
            }
        });
        sex_pop.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
    }
    //日期底部弹出框
    public void date_popupwindow() {
        //加入购物车控件
        //TODO 1:实例化对象
        date_pop = new PopupWindow(getActivity());
        //TODO 2：设置属性
        View view_date = LayoutInflater.from(getActivity()).inflate(R.layout.item_bottom_date, null);
        view_date.findViewById(R.id.tv_item_bottom_date_qx).setOnClickListener(this);
        view_date.findViewById(R.id.tv_item_bottom_date_sure).setOnClickListener(this);
        dp_item_bottom_date = view_date.findViewById(R.id.dp_item_bottom_date);
        // 为日期设置监听事件
//        dp_item_bottom_date.init(year, monthOfYear, dayOfMonth, new DatePicker.OnDateChangedListener() {
//
//            @Override
//            public void onDateChanged(DatePicker view, int year,
//                                      int monthOfYear, int dayOfMonth) {
//                getActivity().this.year = year;
//                monthOfYear = monthOfYear;
//                dayOfMonth = dayOfMonth;
//                Toast.makeText(getContext(), "year -> " + year + "、monthOfYear -> " + monthOfYear + "、dayOfMonth -> " + dayOfMonth, Toast.LENGTH_SHORT).show();
//
//            }
//        });


        date_pop.setContentView(view_date);
        date_pop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        date_pop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置点击外部消失
        date_pop.setOutsideTouchable(true);
        //TODO 3:展示
        /**
         * @param parent 父布局
         * @param gravity gravity可以是Gravity.TOP、Gravity.BOTTOM、Gravity.LEFT、Gravity.RIGHT。。。。
         * @param x x轴偏移量
         * @param y y轴偏移量
         */
        View parent = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_goods_values, null);

        //1.获得当前窗体的属性
        WindowManager.LayoutParams layoutParams = getActivity().getWindow().getAttributes();
        //2.设置透明度为0.5f,半透明状态
        layoutParams.alpha=0.5f;
        //3.为窗体设置新属性
        getActivity().getWindow().setAttributes(layoutParams);
        //4.当窗体消失的时候，恢复透明度
        date_pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams layoutParams = getActivity().getWindow().getAttributes();
                layoutParams.alpha=1f;
                getActivity().getWindow().setAttributes(layoutParams);
            }
        });
        date_pop.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
    }
    //修改属性值
    private void updateUser(){
        p.updateUserRequest(valuesBean.user_id,
                            valuesBean.nick_name,
                            valuesBean.user_sex,
                            valuesBean.user_birthday,
                            valuesBean.user_height,
                            valuesBean.user_weight);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void head_tu(UploadHeadBus bus){
        if (bus.type == BaseEventBusEntity.PHOTO){

            file = getImage(bus.data);
            if (file == null){
                Toast.makeText(getContext(), "个人信息 -> 头像 -> 头库 -> 为空", Toast.LENGTH_SHORT).show();
                return;
            }
            p.uploadHeadRequest(file, valuesBean.user_id);
//            Uri uri = data.getData();
//            imageView.setImageURI(uri);
        }else if (bus.type == BaseEventBusEntity.CAMERA){
            if ("成功".equals(bus.Msg)){
                if (file == null){
                    Toast.makeText(getContext(), "个人信息 -> 头像 -> 拍照 -> 为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                p.uploadHeadRequest(file, valuesBean.user_id);
            }
        }
    }

    /**
     * 封装Uri 和 查询属性
     * @param data  根据Intent获取Uri
     */
    private File getImage(Intent data) {
        String[] projection = {MediaStore.Images.Media.DATA };
        Uri uri = data.getData();
        return getContentProvider(uri,projection);
    }

    /**
     * 从内容提供者获取图片绝对路径
     * 返回File文件
     */
    public File getContentProvider(Uri uri,String[] projection) {
        Cursor cursor = getActivity().getContentResolver().query(uri, projection, null,
                null, null);
        if (null == cursor) {
            return null;
        }else if (cursor.moveToNext()) {
            return new File(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)));
        }else{
            return null;
        }
    }



    //打开图库
    public void startPhoto(){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_PICK);//图库action
        intent.setType("image/*");
        getActivity().startActivityForResult(intent, BaseEventBusEntity.PHOTO);
    }

    //打开相机
    private void startCamera() {
        Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//相机action
        file = new File(Environment.getExternalStorageDirectory(), "user_head.jpg");
        //使用内容提供者向图库中插入数据  DATA 文件路径
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.DATA,file.getAbsolutePath());
        Uri uri = getActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);//设置文件输出uri，必须设置 不然照片不能保存到图库中
        getActivity().startActivityForResult(intent,BaseEventBusEntity.CAMERA);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
